package problems.practice;

import java.util.Stack;

/**
 * 155. 最小栈
 * https://leetcode.cn/problems/min-stack/
 *
 * @author habitplus
 * @since 2022/8/14 9:29
 */
public class T155 {

    class MinStack {

        final Stack<Integer> minStack = new Stack<>();
        final Stack<Integer> dataStack = new Stack<>();

        public MinStack() {
            minStack.push(Integer.MAX_VALUE);
        }

        public void push(int val) {
            dataStack.push(val);
            minStack.push(Math.min(minStack.peek(), val));
        }

        public void pop() {
            if (!dataStack.isEmpty()) {
                dataStack.pop();
                minStack.pop();
            }
        }

        public int top() {
            return dataStack.isEmpty() ? -1 :  dataStack.peek();
        }

        public int getMin() {
            return minStack.isEmpty() ? -1 :  minStack.peek();
        }
    }
}
